package com.yyj.util;

import com.alibaba.fastjson.JSONObject;
import com.github.wxpay.sdk.WXPayUtil;
import com.yyj.util.security.MD5Util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
import java.util.Map;

public class AESUtil {

	/**
	 * 密钥算法
	 */
	private static final String ALGORITHM = "AES";
	/**
	 * 加解密算法/工作模式/填充方式
	 */
	private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";
	/**
	 * 生成key
	 */
	//private static final String key = "your password";//此处为测试key，正式环境请替换成商户密钥
	//private static SecretKeySpec secretKey = new SecretKeySpec(MD5Util.MD5Encode(key, "UTF-8").toLowerCase().getBytes(), ALGORITHM);

	/**
	 * AES加密
	 *
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String encryptData(String data , String key) throws Exception {
		SecretKeySpec secretKey = new SecretKeySpec(MD5Util.MD5Encode(key, "UTF-8").toLowerCase().getBytes(), ALGORITHM);
		// 创建密码器
		Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
		// 初始化
		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		return Base64Util.encode(cipher.doFinal(data.getBytes()));
	}

	/**
	 * AES解密
	 *
	 * @param base64Data
	 * @return
	 * @throws Exception
	 */
	public static String decryptData(String base64Data , String key) throws Exception {
		SecretKeySpec secretKey = new SecretKeySpec(MD5Util.MD5Encode(key, "UTF-8").toLowerCase().getBytes(), ALGORITHM);
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		// 创建密码器
		Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
		// 初始化
		cipher.init(Cipher.DECRYPT_MODE, secretKey);
		return new String(cipher.doFinal(Base64Util.decodeBase64(base64Data)));
	}

	public static void main(String[] args) throws Exception {
		//String A 为测试字符串，是微信返回过来的退款通知字符串
		String A = "depuuU3xMzEe5boCXbUxLfG/hFeQPRXzUc/c5XAOrP91hgEybUBCE0hmYLSn+Kpo90JJsn6aTNW3HfTyttYPbwsfx2auGfOUVNkeipK5rE09n+JgWDAnWVt1of7cpc5WDt/je5wpPCvvIQvugeTevccyw3UtYK1GDHK808qe8pqfZ3hLwJTx7EOhYqHi+Up0eOV9jbXw7aG5WiGljh+0RrHzPtYQqPevMwTpDzDbfOBuVGhEPmBoIMHb0kKg255DrKhdnCnbqGsflpAyIb3JuE+weALLLReNT1BEN0s54Pz8pYcoA44wvkwB6E92/ox+wrzRdaBejPTIME6EcH3D9DWcfksmOKExHojWUPWIyx8SeGl9m0p2HASxxnQE5wBtd8frmQRf3O04U1wKX/FEDJz+Vzu4FfF8Z0n9cvpqHRu6qte0VCwEYLxrufM0M4o9kyychsUIm8Z09f1BDj3PDHyTsrnRza8XXEecl5BM5krqGSP9+RAuUwueRhftayL7K9RyXLX+b7KW2E2qhg0AE39BS961tU8xmzfdWM/DgBlyTMaB6ezBRWahjVkQwyPm1GTEAmzHRVvPJ5BQQU170MD9m5pq8AYQviro8pG3dFsK+GrZhnNa57FTzNsFs55gbVeOmQpmAvWYVF4h4DfOdA1tRf0MryQUkupPnNdneAvoPLpqbNmoJIcUk1FRcoEhV6GZXSiTKNES8ZnmnssvxkenLOPRnjdVzV9OWxPLuAY/2T6+g2d/2CHN8TbKt6+GUwdYNrLkiY/qVajajxn4IMGhL+30U0XHyizo+TuURcSB6kAraAbXRfoY7G4YTmIIfMVwMBSzoN+2ru7wQKjVKlEkuw0tcRTYz6+iZt0J2/JaOuTuTaOcFyBX39IlknMU3Zqj91Qe+FcQq4PVVRRL8SMoJ9xCg5y5wOfk4TSmHktAtnrfg3y6yD/EaIEVe3btMNrrwkUHJlgV9JvZMbZ3YpxNZZ/OYdFtK3oROPXlQXO31gUPVzfEa2p2geo4vYtd6hQEFtZcgiucpieNEoyaxlZ2bcSuK/V0AULGxeuQbW4=";
		String B = AESUtil.decryptData(A , "e1087d6a0fee4dcfac895a4965e4638e");
		System.out.println(B);
		Map<String, String> respMap = WXPayUtil.xmlToMap(B);
		System.out.printf(JSONObject.toJSONString(respMap));
	}

}